package com.wenhao.netease.game;

import java.util.*;

/**
 * @author Wenhao Tong
 * @create 2021-08-07 20:18
 */
public class Main2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int T = scanner.nextInt();
        for (int i = 0; i < T; i++) {
            int N = scanner.nextInt();
            int M = scanner.nextInt();
            int[] order = new int[N];
            List<int[]> list = new ArrayList<>();
            for (int j = 0; j < M; j++) {
                int C = scanner.nextInt();
                int[] temp = new int[C];
                for (int c = 0; c < C; c++) {
                    temp[c] = scanner.nextInt() - 1;
                }
                list.add(temp);
            }
            Set<Integer> visited1 = new HashSet<>();
            Set<Integer> visited2 = new HashSet<>();
            for (int[] x : list) {
                if (visited1.size() == N && visited2.size() == N) {
                    break;
                }
                for (int c = 0; c < x.length; c++) {
                    if (c == 0) {
                        order[x[c]] = order[x[c]] > 0 ? order[x[c]] : 0;
                    } else {
                        order[x[c]] = order[x[c - 1]] + 1 > order[x[c]] ? order[x[c - 1]] + 1 : order[x[c]];
                    }
                    if (visited1.contains(x[c])) {
                        visited2.add(x[c]);
                    } else {
                        visited1.add(x[c]);
                    }
                }
            }
            for (int[] x : list) {
                if (visited2.size() == N) {
                    break;
                }
                for (int c = 0; c < x.length; c++) {
                    if (c == 0) {
                        order[x[c]] = order[x[c]] > 0 ? order[x[c]] : 0;
                    } else {
                        order[x[c]] = order[x[c - 1]] + 1 > order[x[c]] ? order[x[c - 1]] + 1 : order[x[c]];
                    }
                    visited2.add(x[c]);
                }
            }

            int[] result = new int[N];
            boolean flag = true;
            for (int j = 0; j < N; j++) {
                if (result[order[j]] == 0) {
                    result[order[j]] = j + 1;
                } else {
                    System.out.println("NO");
                    flag = false;
                    break;
                }
            }
            if (flag) {
                StringBuilder stringBuilder = new StringBuilder();
                for (int j = 0; j < N; j++) {
                    stringBuilder.append(result[j] + " ");
                }
                System.out.println(stringBuilder);
            }
        }
    }
}
